package com.seaboxdata.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * @author
 * @create 2021-06-04 18:27
 **/
public class HttpUtils {

    public static String doPost(String url, String data) {
        BufferedReader rd = null;
        try {
            if (null != url) {
                StringBuilder buffer = new StringBuilder();
                if (null == data) {
                    data = "";
                }
                URL urlInfo = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) urlInfo.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setReadTimeout(5000000);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                connection.setRequestProperty("user-Agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.4223)");
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
                out.write(data);
                out.flush();
                out.close();
                InputStream in = connection.getInputStream();
                rd = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                String line = null;
                while (null != (line = rd.readLine())) {
                    buffer.append(line);
                }
                return buffer.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rd != null) {
                    rd.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    public static  String doGet(String url,Map<String,String> headerMap) {
        BufferedReader read = null;
        try {
            if (null != url) {
                StringBuilder buffer = new StringBuilder();
                URL realUrl = new URL(url);
                URLConnection connection = realUrl.openConnection();// 打开和URL之间的连接
                for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
                    connection.setRequestProperty(stringStringEntry.getKey(),stringStringEntry.getValue());
                }

                connection.connect();// 建立实际的连接
                read = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = null;
                while (null != (line = read.readLine())) {
                    buffer.append(line);
                }
                return buffer.toString();
            }
        } catch (Exception e) {

        } finally {
            try {
                if (read != null) {
                    read.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }
}

